/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nsc.utilities;

import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.MultipartElement;
import org.apache.struts.upload.MultipartIterator;

/**
 *
 * @author Hue
 */
public class Upload {
    /**
     * The size in bytes of the buffer used to read files from a client upload
     */
    private static final int BUFFER_SIZE = 4096;
    /**
     * The maximum size allowed for a client upload.  A suffix of "K"
     * represents Kilobytes, a suffix of "M" represents "Megabytes", 
     * a suffix of "G" represents Gigabytes, and no suffix is taken
     * as bytes.
     */
    private static final String MAX_FILE_SIZE = "250M";
    private static long UPLOAD_MAX_SIZE = 0;
    
    private static long getDefaultMaxSize() {
        String stringSize = MAX_FILE_SIZE;
        long size = -1;
        int multiplier = 1;
        
        if (stringSize.endsWith("K")) {
            multiplier = 1024;
            stringSize = stringSize.substring(0, stringSize.length()-1);
        }
        if (stringSize.endsWith("M")) {
            multiplier = 1024*1024;
            stringSize = stringSize.substring(0, stringSize.length()-1);
        }
        else if (stringSize.endsWith("G")) {
            multiplier = 1024*1024*1024;
            stringSize = stringSize.substring(0, stringSize.length()-1);
        }
        
        size = Long.parseLong(stringSize);

        return (size * multiplier) < UPLOAD_MAX_SIZE ? UPLOAD_MAX_SIZE : (size * multiplier);
    }
    
    public static void setUploadMaxSize(final long maxSize) {
        UPLOAD_MAX_SIZE = maxSize;
    }
    
    public static MultipartIterator getMultipartIterator(final HttpServletRequest request) throws ServletException{
        return new MultipartIterator(request,BUFFER_SIZE,getDefaultMaxSize(),System.getProperty("java.io.tmpdir"));
    }
   
    public static Hashtable getMultipartParameters(final HttpServletRequest request) throws Exception {
        return getMultipartParameters(request,true);
    }
    
    public static Hashtable getMultipartParameters(final HttpServletRequest request, final boolean bReadData) throws Exception {
        final Hashtable parameters = new Hashtable();
        final MultipartIterator iterator = getMultipartIterator(request);
        MultipartElement element = null;
        while ((element = iterator.getNextElement()) != null) {
            if(element.isFile()) {
                byte[] data = null;
                final File f = element.getFile();
                if(bReadData && f != null) {
                    final FileInputStream in = new FileInputStream(f);
                    data = Util.readData(in);
                    in.close();
                }
                parameters.put(element.getName(),new FileObject(element.getFileName(),(f==null?"":f.getPath()),data)); 
            }
            else {
                parameters.put(element.getName(),element.getValue());
            }
        }
        
        return parameters;
    }
}
